package com.frcdi.qms.system.service.impl;

import java.util.List;

import com.frcdi.qms.common.utils.SecurityUtils;
import com.frcdi.qms.masterdata.domain.File;
import com.frcdi.qms.masterdata.mapper.FileMapper;
import com.frcdi.qms.system.domain.QmsMessage;
import com.frcdi.qms.system.mapper.QmsMessageMapper;
import com.frcdi.qms.system.service.IQmsMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * messageService业务层处理
 *
 * @author Mr.han
 * @date 2024-05-15
 */
@Service
public class QmsMessageServiceImpl implements IQmsMessageService
{
    @Autowired
    private QmsMessageMapper qmsMessageMapper;

    @Autowired
    private FileMapper fileMapper;

    /**
     * 查询message
     *
     * @param messageId message主键
     * @return message
     */
    @Override
    public QmsMessage selectQmsMessageByMessageId(Long messageId)
    {
        return qmsMessageMapper.selectQmsMessageByMessageId(messageId);
    }

    /**
     * 查询message列表
     *
     * @param qmsMessage message
     * @return message
     */
    @Override
    public List<QmsMessage> selectQmsMessageList(QmsMessage qmsMessage)
    {
        return qmsMessageMapper.selectQmsMessageList(qmsMessage);
    }

    /**
     * 新增message
     *
     * @param qmsMessage message
     * @return 结果
     */
    @Transactional
    @Override
    public int insertQmsMessage(QmsMessage qmsMessage) {
        int row = qmsMessageMapper.insertQmsMessage(qmsMessage);
        List<QmsMessage> messages = qmsMessageMapper.selectQmsMessageList(qmsMessage);
        if (row > 0) {
            List<File> fileList = qmsMessage.getFileList();
            if (fileList != null && fileList.size() > 0) {
                for (File file : fileList) {
                    file.setTableNo(messages.get(0).getMessageId().toString());
                    file.setCreateBy(SecurityUtils.getLoginUser().getUsername());
                    fileMapper.insertFile(file);
                }
            }


        }
        return row;

    }

    /**
     * 修改message
     *
     * @param qmsMessage message
     * @return 结果
     */
    @Override
    public int updateQmsMessage(QmsMessage qmsMessage)
    {
        int row = qmsMessageMapper.updateQmsMessage(qmsMessage);
        if (row > 0) {
            //  先将已有的删除 再重新插入新的文件数据
            List<File> fileList = qmsMessage.getFileList();
            if (fileList != null && fileList.size() > 0) {
                fileMapper.deleteFileByTableNo(qmsMessage.getMessageId().toString());
                for (File file : fileList) {
                    file.setTableNo(qmsMessage.getMessageId().toString());
                    file.setCreateBy(SecurityUtils.getLoginUser().getUsername());
                    fileMapper.insertFile(file);
                }
            }
        }
        return row;
    }

    /**
     * 批量删除message
     *
     * @param messageIds 需要删除的message主键
     * @return 结果
     */
    @Override
    public int deleteQmsMessageByMessageIds(Long[] messageIds)
    {
        return qmsMessageMapper.deleteQmsMessageByMessageIds(messageIds);
    }

    /**
     * 删除message信息
     *
     * @param messageId message主键
     * @return 结果
     */
    @Override
    public int deleteQmsMessageByMessageId(Long messageId)
    {
        return qmsMessageMapper.deleteQmsMessageByMessageId(messageId);
    }
}
